$(function () {
    obj.init();
});
var obj = new JsFun();
function JsFun() {
    return {
        form: null,
        init: function () {
            this.form = new xhForm($('#modify_form'));
            this.form.parse().bindRequired($('#bt_modal_confirm',parent.document));
            this.funObj().addModelData();//获取select数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#model').unbind('change').on('change',function () {//修改类型
                $this.funObj().getProd();
            });
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                addModelData: function () {
                    xhExtra.ajaxRequest('device/getTabletData', {}, {check:true}, function (data) {
                        $this.funObj().fillSelect('model',data.model);
                    });
                },
                fillSelect: function (selector,data,id,text) {
                    var str = '<option value="" style="display: none" selected>请选择</option>';
                    if(id != null && text != null){
                        $.each(data,function (key,val) {
                            str += '<option value="'+val[id]+'">'+val[text]+'</option>';
                        })
                    } else {
                        $.each(data,function (key,val) {
                            if (val === $('#model_data').val()){
                                str += '<option value="'+key+'" selected>'+val+'</option>';
                            }else{
                                str += '<option value="'+key+'">'+val+'</option>';
                            }
                        })
                    }
                    $('#'+selector).empty().append(str);
                },
                getProd: function () {
                    var sModel = $('#model').val();
                    xhExtra.ajaxRequest('device/getBrandData', {model:sModel}, {check:false}, function (data) {
                        if (data !== 'null'){
                            $("#sBrand").val(data);
                        } else {
                            $("#sBrand").val('');
                        }
                    });
                },
                saveData:function () {
                    var id = $('#id_data').val();
                    var type = $('#type_data').val();
                    var sRemark = $('#sRemark').val();
                    var fatherName = $('#fatherName').val();
                    var fatherPhone = $('#fatherPhone').val();
                    var motherName = $('#motherName').val();
                    var motherPhone = $('#motherPhone').val();
                    var guardName = $('#guardName').val();
                    var guardPhone = $('#guardPhone').val();
                    var model = $('#model').val();
                    var deviceNumber =  $('#deviceNumber').val();
                    var sBrand =  $('#sBrand').val();
                    var sImei =  $('#sImei').val();
                    var falseMsg = '';
                    var phone = /(^[1][3,4,5,7,8][0-9]{9}$)|(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)/;//电话或手机号
                    if (sRemark === '' || model === '' || deviceNumber === '' || sBrand === '' || sImei === ''){
                        if (sRemark === '') falseMsg = '备注不能为空';
                        if (model === '') falseMsg = '设备不能为空';
                        if (deviceNumber === '') falseMsg = '设备号不能为空';
                        if (sBrand === '') falseMsg = '品牌不能为空';
                        if (sImei === '') falseMsg = 'IMei不能为空';
                        window.top.xhTop.showError('填写完整信息:' + falseMsg);
                        xhExtra.setBtnNormal($('#bt_modal_confirm', parent.document));
                        return false;
                    }
                    var parameter = null;
                    if (type == 1){
                        if ((fatherName.length !==0 && fatherPhone.length !== 0 ) || (motherName.length !==0 && motherPhone.length !== 0 ) || (guardName.length !==0 && guardPhone.length !== 0 )) {
                            if (fatherPhone.length !== 0) {
                                var isPhone1 = phone.test(fatherPhone);
                                if (isPhone1 === false) {
                                    window.top.xhTop.showError('请填入爸爸的正确手机号');
                                    xhExtra.setBtnNormal($('#bt_modal_confirm', parent.document));
                                    return false;
                                }
                            }
                            if (motherPhone.length !== 0) {
                                var isPhone2 = phone.test(motherPhone);
                                if (isPhone2 === false) {
                                    window.top.xhTop.showError('请填入妈妈的正确手机号');
                                    xhExtra.setBtnNormal($('#bt_modal_confirm', parent.document));
                                    return false;
                                }
                            }
                            if (guardPhone.length !== 0) {
                                var isPhone3 = phone.test(guardPhone);
                                if (isPhone3 === false) {
                                    window.top.xhTop.showError('请填入监护人的正确手机号');
                                    xhExtra.setBtnNormal($('#bt_modal_confirm', parent.document));
                                    return false;
                                }
                            }
                            parameter = {id:id, sRemark:sRemark, fatherName:fatherName, fatherPhone:fatherPhone,
                                motherName:motherName, motherPhone:motherPhone, guardName:guardName, guardPhone:guardPhone, model: model, deviceNumber: deviceNumber,sBrand:sBrand,sImei:sImei};
                        }else{
                            window.top.xhTop.showError('至少完整填写一个父母或监护人的信息！');
                            xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                            return false;
                        }
                    }else{
                        parameter = {id:id, sRemark:sRemark, model: model, deviceNumber: deviceNumber,sBrand:sBrand,sImei:sImei};
                    }
                    xhExtra.ajaxRequest('Device/saveRepairInfo', parameter,
                        {success: true,check:false},function (res) {
                            if(res.code === 200){
                                window.top.swal('成功',res.msg,'success');
                                window.top.xhTop.closeModal();
                                window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                            } else {
                                window.top.xhTop.showError(res.error);
                                xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                            }
                        });
                }
            }
        }
    }
}